package cn.springcloud.fix.auth.granter;

import cn.springcloud.fix.auth.dao.entity.User;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.OAuth2RequestFactory;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;

import java.util.Map;

public class MobileSmsTokenGranter extends AbstractCustomTokenGranter {

    private static final String GRANT_TYPE = "sms";

    private final CustomUserDetailsService customUserDetailsService;

    public MobileSmsTokenGranter(CustomUserDetailsService userDetailsService, AuthorizationServerTokenServices tokenServices, ClientDetailsService clientDetailsService, OAuth2RequestFactory requestFactory) {
        super(tokenServices, clientDetailsService, requestFactory, GRANT_TYPE);
        this.customUserDetailsService = userDetailsService;
    }

    @Override
    protected User getCustomUser(Map<String, String> parameters) {
        String mobile = parameters.get("mobile");
        String smscode = parameters.get("smscode");
        return customUserDetailsService.loadUserByMobileAndSmscode(mobile, smscode);
    }
}
